﻿using MachineVision.API.Models;
using MachineVision.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace MachineVision.API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    //[Authorize]//(Roles = "Admin")
    public class AdminController : ControllerBase
    {
        private readonly ILogger<AdminController> logger;
        private readonly IMenuService mService;

        public AdminController(ILogger<AdminController> logger, IMenuService mService)
        {
            this.logger = logger;
            this.mService = mService;
        }
        // GET: api/<MenuController>
        [HttpPost]
        public ApiResult Login([FromBody] TokenData tokenData)
        {
            ApiResult result = new ApiResult();

            try
            {
                //SerConnectState 已连接  
                //if (_apiReport != 0 && SerConnectState)
                //{
                var apiurl = "http://121.89.194.47:5000/connect/token";
                Dictionary<string, string> paramsparams = new Dictionary<string, string>();
                paramsparams.Add("grant_type", "password");
                paramsparams.Add("client_id", "client");
                paramsparams.Add("client_secret", "secret");
                paramsparams.Add("username", tokenData.username);
                paramsparams.Add("password", tokenData.password);
                string re = HttpXmlClient.Post(apiurl, paramsparams, null);


                if (!string.IsNullOrEmpty(re))
                {
                    TokenInfo jo = JsonConvert.DeserializeObject<TokenInfo>(re);
                    jo.name = tokenData.username;
                    result.data = jo;
                    return result;
                }
                else
                {
                    result.code = 0;
                    result.msg = "权限错误";
                    return result;
                }
                //}
            }
            catch (Exception ex)
            {
                result.code = 0;
                result.msg = "失败";
                return result;
            } 
        }

        //// GET api/<MenuController>/5
        //[HttpGet("{id}")]
        //public string Get(int id)
        //{
        //    return "value";
        //}

        //// POST api/<MenuController>
        //[HttpPost]
        //public void Post([FromBody] string value)
        //{
        //}

        //// PUT api/<MenuController>/5
        //[HttpPut("{id}")]
        //public void Put(int id, [FromBody] string value)
        //{
        //}

        //// DELETE api/<MenuController>/5
        //[HttpDelete("{id}")]
        //public void Delete(int id)
        //{
        //}
    }
    public class TokenData
    { 
        public string username { get; set; }
        public string password { get; set; }
        //public string grant_type { get; set; } = "password";
        //public string client_id { get; set; } = "client";
        //public string client_secret { get; set; } = "secret";
    }
    public class TokenInfo { 
        public string access_token { get; set; }
        public string expires_in { get; set; }
        public string token_type { get; set; }
        public string scope { get; set; }
        public string name { get; set; }

    }
}
